package com.wolf.system.service.impl;

import com.wolf.constant.SessionKey;
import com.wolf.vo.PageResult;
import com.wolf.provider.order.enumerate.QUERY_TYPE;
import com.wolf.provider.order.info.PageBean;
import com.wolf.provider.order.info.PageInfo;
import com.wolf.provider.order.info.ParamBean;
import com.wolf.provider.order.service.impl.BaseServiceImpl;
import com.wolf.system.model.SysUser;
import com.wolf.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<Long> implements ISysUserService {

    /**
     * 新增
     * @param model
     * @return
     */
    @Override
    public boolean addSysUser(SysUser model){
        return insertModel(model);
    }

    /**
     * 修改
     * @param model
     * @return
     */
    @Override
    public boolean updateSysUser(SysUser model){
        return updateModel(model);
    }

    /**
     * 删除
     * @param model
     * @return
     */
    @Override
    public boolean deleteSysUser(SysUser model){
        return deleteModel(model);
    }

    @Override
    public SysUser findSysUser(String userName, String userPwd) {
        StringBuilder sql = new StringBuilder("SELECT id,user_name AS userName,user_pwd AS user_pwd,real_name AS realName,telephone,");
        sql.append("`status`,create_time AS createTime,creator,modify_person AS modifyPerson,modify_time AS modifyTime ");
        sql.append("FROM sys_user t WHERE t.user_name = #{user_name} AND t.user_pwd = #{user_pwd}");

        Map<String,Object> map = new HashMap<>();
        map.put("user_name",userName);
        map.put("user_pwd",userPwd);
        List<SysUser> list = findModelListByMap(sql.toString(),map,new SysUser());
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }

    @Override
    public SysUser findSysUserByUserName(String userName) {
        StringBuilder sql = new StringBuilder("SELECT id,user_name AS userName,user_pwd AS user_pwd,real_name AS realName,telephone,");
        sql.append("`status`,create_time AS createTime,creator,modify_person AS modifyPerson,modify_time AS modifyTime ");
        sql.append("FROM sys_user t WHERE t.user_name = #{user_name}");
        Map<String,Object> map = new HashMap<>();
        map.put("user_name",userName);
        List<SysUser> list = findModelListByMap(sql.toString(),map,new SysUser());
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }


    @Override
    public PageResult findByPage(SysUser model, PageBean pageBean) {
        StringBuilder sql = new StringBuilder("SELECT id,user_name AS userName,user_pwd AS userPwd,real_name AS realName,telephone,");
        sql.append("`status`,create_time AS createTime,creator,modify_person AS modifyPerson,modify_time AS modifyTime ");
        sql.append("FROM sys_user t WHERE 1=1 ");

        List<ParamBean> paramBeans = new ArrayList<ParamBean>();
        if(StringUtils.isNotEmpty(model.getUserName())){
            ParamBean paramBean = new ParamBean();
            paramBean.setWhereField("t.user_name");
            paramBean.setField("user_name");
            paramBean.setValue(model.getUserName());
            paramBean.setType(QUERY_TYPE.EQUAL.getType());
            paramBeans.add(paramBean);
        }
        if(StringUtils.isNotEmpty(model.getStatus())){
            ParamBean paramBean = new ParamBean();
            paramBean.setWhereField("t.status");
            paramBean.setField("status");
            paramBean.setValue(model.getStatus());
            paramBean.setType(QUERY_TYPE.EQUAL.getType());
            paramBeans.add(paramBean);
        }

        PageInfo<SysUser> vos = findByPageInfo(sql.toString(),paramBeans,model,pageBean);

        PageResult result = new PageResult();
        result.buildResult(vos);
        return result;
    }

    @Override
    public List<SysUser> findSysUserList() {
        StringBuilder sql = new StringBuilder("SELECT id,user_name AS userName,user_pwd AS userPwd,real_name AS realName,telephone,");
        sql.append("`status`,create_time AS createTime,creator,modify_person AS modifyPerson,modify_time AS modifyTime ");
        sql.append("FROM sys_user t WHERE 1=1 ");

        Map<String,Object> map = new HashMap<>();
        List<SysUser> list = findModelListByMap(sql.toString(),map,new SysUser());

        return list;
    }

    @Override
    public SysUser getSessionSysUser(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpSession session = request.getSession(true);
        SysUser sysUser = (SysUser)session.getAttribute(SessionKey.USERINFO.getCode());
        return sysUser;
    }
}
